package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.user.entity.PayChannel;
import com.yike.user.mapper.PayChannelMapper;
import com.yike.user.param.PayChannelParam;
import com.yike.user.service.PayChannelService;
import com.yike.user.param.PayChannelPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.PayChannelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 支付渠道 服务实现类
 *
 * @author Sakura
 * @since 2024-11-15
 */
@Slf4j
@Service
public class PayChannelServiceImpl extends BaseServiceImpl<PayChannelMapper, PayChannel> implements PayChannelService {

    @Autowired
    private PayChannelMapper payChannelMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean savePayChannel(PayChannelParam payChannelParam) throws Exception {
        // 先判断名称是否已存在
        PayChannel payChannel = payChannelMapper.selectOne(Wrappers.lambdaQuery(PayChannel.class)
                .eq(PayChannel::getName, payChannelParam.getName()));
        if (payChannel != null) {
            throw new BusinessException("当前服务商已存在");
        }

        payChannel = new PayChannel();
        BeanUtils.copyProperties(payChannelParam, payChannel);
        payChannel.setCreateBy(LoginUtil.getUserId());
        return super.save(payChannel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updatePayChannel(PayChannelParam payChannelParam) throws Exception {
        PayChannel payChannel = payChannelMapper.selectById(payChannelParam.getId());
        if (payChannel == null) {
            throw new BusinessException("服务商信息异常");
        }
        // 防止出现修改后名称重复
        Integer checkNum = payChannelMapper.selectCount(Wrappers.lambdaQuery(PayChannel.class)
                .eq(PayChannel::getName, payChannelParam.getName())
                .ne(PayChannel::getId, payChannelParam.getId()));
        if (checkNum > 0) {
            throw new BusinessException("当前服务商名称重复");
        }

        BeanUtils.copyProperties(payChannelParam, payChannel);
        payChannel.setUpdateBy(LoginUtil.getUserId());
        return super.updateById(payChannel);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deletePayChannel(Long id) throws Exception {
        return super.removeById(id);
    }

    @Override
    public PayChannelVo getPayChannel(Long id) throws Exception {
        PayChannel payChannel = payChannelMapper.selectById(id);
        if (payChannel == null) {
            throw new BusinessException("服务商信息异常");
        }

        PayChannelVo payChannelVo = new PayChannelVo();
        BeanUtils.copyProperties(payChannel, payChannelVo);
        return payChannelVo;
    }

    @Override
    public Paging<PayChannelVo> getPayChannelPageList(PayChannelPageParam payChannelPageParam) throws Exception {
        Page<PayChannel> page = new PageInfo<>(payChannelPageParam);
        IPage<PayChannelVo> iPage = payChannelMapper.getPayChannelList(page, payChannelPageParam);
        return new Paging<PayChannelVo>(iPage);
    }

}
